package com.sumainfor.pc.market.service;

import com.baomidou.mybatisplus.generator.config.IFileCreate;
import com.sumainfor.common.Constants;
import com.sumainfor.common.util.ContextUtils;
import com.sumainfor.common.util.JsonResult;
import com.sumainfor.common.utlis.ComUtils;
import com.sumainfor.common.utlis.ParamsCommon;
import com.sumainfor.pc.market.dao.DmMediumMapper;
import com.sumainfor.pc.market.dao.RdBankMapper;
import com.sumainfor.pc.market.dao.UtilsMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @Date 2018年11月7日
 * @Describe 中介
 */
@Service
public class DmMediumService implements Serializable {

    private ReentrantLock lock = new ReentrantLock(false);

    Logger log = LoggerFactory.getLogger(DmMediumService.class);// 日志

    @Autowired
    DmMediumMapper dmMediumMapper;
    @Autowired
    UtilsMapper utilsMapper;
    String UUID=null;
    /**
     * 分页查询中介列表
     */
    public List<Map<String,Object>> getMediumList(Map<String,Object> params){
        return dmMediumMapper.getMediumList(params);
    }
    /**
     * 查询中介总数
     */
    public Integer getMediumListCount(Map<String,Object> params)
    {
        return dmMediumMapper.getMediumListCount(params);
    }
    /**
     * 新增中介
     */
    public Integer addMedium(Map<String,Object> params){
        Integer result=0;
        /**
         * 加锁操作
         */
        lock.lock();
        try{
            try {
                //工单编号
                Map<String,Object>uuid=utilsMapper.getUUID(new HashMap<String,Object>(){{
                    put("uuid", "MeId");
                    put("database", Constants.DATA_SOURCE_TCMKT);
                    put("surface", "tcmkt_medium");
                }});
                if(uuid!=null &&(!uuid.get("uuid").toString().equals(""))){
                    int number=Integer.valueOf(uuid.get("uuid").toString());
                    UUID= ComUtils.getLocalTrmSeqNum("me",number);
                }else{
                    UUID=ComUtils.getLocalTrmSeqNum("me",0);
                }
                params.put("by", ContextUtils.getUserId());//操作人
                params.put("MeId",UUID);
                params.put("term", "2");// 创建端 Insert端： 1 租客端 2市场部 3 工程部   4运营部   5财务部  6总经办 7发展部
                params.put("fnc",
                        this.getClass().getSimpleName() + "." + Thread.currentThread().getStackTrace()[1].getMethodName());// 创建Function名
                ParamsCommon.commonInsertParms(params);
                Map<String,Object> map=(Map<String,Object>)params.get("data");
                if(map!=null){
                    params.putAll(map);
                    result=dmMediumMapper.addMedium(params);
                    return  result;
                }else{
                    return 0;
                }
            } catch (Exception e) {
                e.printStackTrace();
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            }
        }
        finally {
            lock.unlock();
        }
        return result;
    }
    /**
     * 查询中介列表
     */
    public List<Map<String,Object>> getMeIdist(Map<String,Object> params){
        return dmMediumMapper.getMeIdist(params);
    }
    /**
     * 根据中介的编号查询中介的信息
     */
    public Map<String,Object> getMeInfo(Map<String,Object> params){
        return dmMediumMapper.getMeInfo(params);
    }
    /**
     *查询某个中介介绍的租客签约次数按月份分组
     */
    public JsonResult getMeTentByMonth(Map<String,Object> params){
        JsonResult result=new JsonResult();
        if (params.get("SysUserId")==null){
            result.putFailed("参数不正确");
        }
        List<Map<String,Object>> list=dmMediumMapper.getMeTentByMonth(params);
        if (list!=null){
            result.put(list);
        }else{
            result.putFailed("服务器繁忙");
        }
        return result;
    }
    /**
     *查询中介介绍的租客有签约的年份
     */
    public JsonResult getMeMonthList(Map<String,Object> params){
        JsonResult result=new JsonResult();
        if (params.get("SysUserId")==null){
            result.putFailed("参数不正确");
        }
        List<Map<String,Object>> list=dmMediumMapper.getMeMonthList(params);
        if (list!=null){
            result.put(list);
        }else{
            result.putFailed("服务器繁忙");
        }
        return result;
    }

}
